﻿using System;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.Product;
using ZBJF.Warranty.WebProvider;

namespace ZBJF.Warranty.AdminSite.Controllers
{
    public class ProductTypeController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Index(ProductTypeModel model)
        {
            var total = 0;
            var list = ServiceFactory.ProductTypeServiceInstance.GetPagingList(model, out total).AsNoTracking().ToList().Select(o => new
            {
                o.ID,
                o.ProductName,
                o.CreateBy,
                CreateTime = o.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                o.IsAvailable,
                LastModifyTime = o.LastModifyTime.ToString("yyyy-MM-dd HH:mm:ss"),
                o.LastModifyBy,
                o.Sort,
                o.ProductCode
            });
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Edit(int id)
        {
            var productBrand = ServiceFactory.ProductTypeServiceInstance.LoadByID(id);
            return View(productBrand);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SaveProductType(ProductType model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.ID > 0 ? OpreateType.Update : OpreateType.Add
            };
            var serv = ServiceFactory.ProductTypeServiceInstance;

            if (model.ID == 0)
            {
                if (serv.IsExist(model.ProductName))
                {
                    responseResult.Message = "该产品已经存在";
                }
                else
                {
                    model.CreateTime = DateTime.Now;
                    model.CreateBy = CurrentUser.UserAccount;
                    model.LastModifyBy = CurrentUser.UserAccount;
                    model.LastModifyTime = DateTime.Now;
                    var entity = serv.AddEntities(model);
                    responseResult.Success = entity.ID > 0;
                    //设置编码
                    if (responseResult.Success)
                    {
                        entity.ProductCode = CommonMethod.GenerateCode("SBLX", entity.ID.ToString(), 6);
                        serv.UpdateEntities(entity);
                    }
                }
            }
            else
            {
                var entity = serv.LoadByID(model.ID);
                if (entity != null)
                {
                    if (entity.ProductName != model.ProductName && serv.IsExist(model.ProductName, model.ID))
                    {
                        responseResult.Message = "已存在同名的产品，请修改产品名";
                    }
                    else
                    {
                        //entity = ReflectHelper<ProductType, ProductType>.CloneT1ToT2(model, entity);
                        entity.ProductName = model.ProductName;
                        entity.Sort = model.Sort;
                        entity.IsAvailable = model.IsAvailable;
                        entity.LastModifyBy = CurrentUser.UserAccount;
                        entity.LastModifyTime = DateTime.Now;
                        responseResult.Success = ServiceFactory.ProductTypeServiceInstance.UpdateEntities(entity);
                    }
                }
                else
                {
                    responseResult.Message = "该产品不存在或已被删除";
                }
            }
            TempData["ResponseResult"] = responseResult;
            return responseResult.Success ? RedirectToAction("Index") : RedirectToAction("Edit", new { id = model.ID });
        }

        /// <summary>
        /// 启用或禁用品牌
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isAvailable"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateStatus(int id, bool isAvailable)
        {
            var entity = ServiceFactory.ProductTypeServiceInstance.LoadByID(id);
            var success = false;
            if (entity != null)
            {
                entity.IsAvailable = isAvailable;
                entity.LastModifyBy = CurrentUser.UserAccount;
                entity.LastModifyTime = DateTime.Now;
                success = ServiceFactory.ProductTypeServiceInstance.UpdateEntities(entity);
            }
            return Json(new
            {
                success = success,
                message = success ? "修改成功" : "修改失败"
            });
        }

        #region 获取所有的设备类型 + JsonResult GetAllProductType()
        public JsonResult GetAllProductType()
        {
           var entities= ServiceFactory.ProductTypeServiceInstance.LoadEntities(o => o.IsAvailable == true).Select(t => new
            {
                t.ID,
                t.ProductName
            }).AsNoTracking().ToList();
            return Json(new {Result=true, Data = entities }, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
